package com.jlt.dao.dic;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.jlt.entity.dic.DicEntity;

/**
 * 字典类型对象的泛型DAO.
 * 
 * @author zhoutao
 */
@Component
public class DicEntityDao extends HibernateDao<DicEntity, Long> {

	//根据dicType搜索
	public Page<DicEntity> findPage(Page<DicEntity> page,
			List<PropertyFilter> filters, Long dicTypeId) {
		List<Criterion> cList = new ArrayList<Criterion>();
		Criterion[] criterions = buildCriterionByPropertyFilter(filters);

		if (null != dicTypeId && -1 != dicTypeId) {
			for (Criterion c : criterions) {
				cList.add(c);
			}
			Criterion criterion = Restrictions.eq("dicType.id", dicTypeId);
			cList.add(criterion);
			criterions = cList.toArray(new Criterion[cList.size()]);
		}
		return super.findPage(page, criterions);
	}
	
	public DicEntity getDicEntityByCode(String code){
		return super.findUniqueBy("code", code);
	}
	
	public List<DicEntity> findDicEntityByTypeCode(String typeCode ) {
		String hql="from DicEntity where dicType.code =? order by orderNo";
		return super.find(hql,typeCode);
	}
	
	public List<DicEntity> findDicEntityByTypeId(Long dicTypeId) {
		String hql="from DicEntity where dicType.id =? order by orderNo";
		return super.find(hql,dicTypeId);
	}
}
